package com.g5.edu.config;


import com.g5.edu.service.impl.DfUsersInfoServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

@Configuration
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private DfUsersInfoServiceImpl dfUsersInfoService;

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(dfUsersInfoService);

    }

    @Override
    protected void configure(HttpSecurity http) throws Exception{
        http.csrf().disable()
                .authorizeRequests()
                .antMatchers("/css/*",
                        "/js/**",
                        "/images/*",
                        "/font/*",
                        "/views/login",        //登录页面
                        "/views/register",     //注册页面
                        "/views/index",        //首页（页面跳页面）
                        "/index",               //首页（从后台直接跳转页面）
                        "/login",              //登录的后台接口
                        "/register",           //注册的后台接口
                        "/demo",
                        "/edu/dfStudyFile/file_list",          //文件列表
                        "/"                    //首页
                        )
                .permitAll()     //以上路径不需要登录就能直接访问
                .anyRequest()
                .authenticated()
                .and()
                .formLogin()
                .loginPage("/views/login")         // 配置登录时显示的页面
                .loginProcessingUrl("/login")           // 登录,后台接口
                .failureUrl("/views/login")        // 登录失败时跳转的页面
                .defaultSuccessUrl("/views/index") // 登录成功跳转的页面
                .and()
                .logout()
                .logoutUrl("/logout") // 配置登出的接口
                .logoutSuccessUrl("/views/login") // 登出后跳转回登录页面
                .and().csrf().disable();
    }
}
